package com.hrsoft.modules.bid.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.hrsoft.modules.bid.dto.GoodsTreeDto;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hrsoft.modules.bid.entity.GoodsCategoryEntity;
import com.hrsoft.modules.bid.service.GoodsCategoryService;
import com.hrsoft.common.utils.PageUtils;
import com.hrsoft.common.utils.R;



/**
 * 产品分类
 *
 * @author garfield.yue
 * @email yuedefeng101@163.com
 * @date 2020-04-30 10:02:32
 */
@RestController
@RequestMapping("bid/goodscategory")
public class GoodsCategoryController {
    @Autowired
    private GoodsCategoryService goodsCategoryService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions(" bid:goodscategory:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = goodsCategoryService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions(" bid:goodscategory:info")
    public R info(@PathVariable("id") Integer id){
		GoodsCategoryEntity goodsCategory = goodsCategoryService.getById(id);

        return R.ok().put("goodsCategory", goodsCategory);
    }

    @RequestMapping("/tree-list/{parentId}")
    public R treeList(@PathVariable("parentId") Integer parentId){
        GoodsTreeDto goodsTreeDto = new GoodsTreeDto();
        goodsTreeDto.setLabel("根节点");
        goodsTreeDto.setId(0);
        List<GoodsTreeDto> goodsTreeDtos = goodsCategoryService.getTreeList(parentId);
        goodsTreeDto.setChildren(goodsTreeDtos);
        List<GoodsTreeDto> rootList = new ArrayList<>();
        rootList.add(goodsTreeDto);
        return R.ok().put("goodsTree", rootList);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions(" bid:goodscategory:save")
    public R save(@RequestBody GoodsCategoryEntity goodsCategory){
		goodsCategoryService.save(goodsCategory);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions(" bid:goodscategory:update")
    public R update(@RequestBody GoodsCategoryEntity goodsCategory){
		goodsCategoryService.updateById(goodsCategory);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions(" bid:goodscategory:delete")
    public R delete(@RequestBody Integer[] ids){
		goodsCategoryService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
